package org.s3.project.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.s3.project.entity.Orders;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author cfx
 * @version 1.0
 * @Description
 * @date 2025/2/9 9:45
 */
@Mapper
@Repository
public interface OrdersMapper extends BaseMapper<Orders> {
    //查询所有
    @Select(" select o.id as o_id, o.number as o_number , o.status , u.name as u_name, u.phone , m.name as m_name, m.contact_phone, o.amount as o_amount , o.order_time , \n" +
            "o.checkout_time , o.pay_method , o.pay_status , o.remark , o.cancel_reason , o.rejection_reason , o.cancel_time , o.estimated_delivery_time , o.delivery_status ,\n" +
            " o.delivery_time , o.pack_amount , o.tableware_number , o.tableware_status ,o.pingjia as ping_jia, od.dish_id , d.name as d_name , d.price as d_price , d.image , od.number , od.amount , \n" +
            " od.dish_flavor , s.id , s.name , s.price , sc.copies, ab.longitude as ulongitude,m.longitude as mlongitude,m.Latitude as mLatitude, ab.latitude as ulatitude, ab.consignee, ab.sex, ab.phone, ab.city_name, ab.district_name, ab.detail, ab.label,\n" +
            "  ab.is_default, m.rejection_reason, m.operational_reason, d.description, d.id as d_id from orders o left join user u on o.user_id = u.id left join address_book ab on \n" +
            "  o.address_book_id = ab.id left join order_detail od on o.id = od.order_id left join dish d on od.dish_id = d.id left join setmeal s on od.setmeal_id = s.id left join \n" +
            "  setmeal_dish sc on s.id = sc.setmeal_id and od.dish_id = sc.dish_id left join merchant m on o.merchant_id = m.id where u.id = #{id} order by o.order_time desc; ")
    List<Orders> selectOrders(@Param("id")int id);
    //查询待评价的订单
    @Select(" select o.id as o_id, o.number as o_number , o.status , u.name as u_name, u.phone , m.name as m_name, m.contact_phone, o.amount as o_amount , o.order_time , \n" +
            "o.checkout_time , o.pay_method , o.pay_status , o.remark , o.cancel_reason , o.rejection_reason , o.cancel_time , o.estimated_delivery_time , o.delivery_status ,\n" +
            " o.delivery_time , o.pack_amount , o.tableware_number , o.tableware_status ,o.pingjia as ping_jia, od.dish_id , d.name as d_name , d.price as d_price , d.image , od.number , od.amount , \n" +
            " od.dish_flavor , s.id , s.name , s.price , sc.copies, ab.longitude as ulongitude,m.longitude as mlongitude,m.Latitude as mLatitude, ab.latitude as ulatitude, ab.consignee, ab.sex, ab.phone, ab.city_name, ab.district_name, ab.detail, ab.label,\n" +
            "  ab.is_default, m.rejection_reason, m.operational_reason, d.description, d.id as d_id from orders o left join user u on o.user_id = u.id left join address_book ab on \n" +
            "  o.address_book_id = ab.id left join order_detail od on o.id = od.order_id left join dish d on od.dish_id = d.id left join setmeal s on od.setmeal_id = s.id left join \n" +
            "  setmeal_dish sc on s.id = sc.setmeal_id and od.dish_id = sc.dish_id left join merchant m on o.merchant_id = m.id where u.id = #{id} and o.pingjia=0 and o.status=5  order by o.order_time desc; ")
  List<Orders> ShowEvaluate(@Param("id")int id);

    //查询退款订单
    @Select(" select o.id as o_id, o.number as o_number , o.refund_reason,o.rejection_reason,o.status , u.name as u_name, u.phone , m.name as m_name, m.contact_phone, o.amount as o_amount , o.order_time , \n" +
            "o.checkout_time , o.pay_method , o.pay_status , o.remark , o.cancel_reason , o.rejection_reason , o.cancel_time , o.estimated_delivery_time , o.delivery_status ,\n" +
            " o.delivery_time , o.pack_amount , o.tableware_number , o.tableware_status ,o.pingjia as ping_jia, od.dish_id , d.name as d_name , d.price as d_price , d.image , od.number , od.amount , \n" +
            " od.dish_flavor , s.id , s.name , s.price , sc.copies, ab.longitude as ulongitude,m.longitude as mlongitude,m.Latitude as mLatitude, ab.latitude as ulatitude, ab.consignee, ab.sex, ab.phone, ab.city_name, ab.district_name, ab.detail, ab.label,\n" +
            "  ab.is_default, m.rejection_reason, m.operational_reason, d.description, d.id as d_id from orders o left join user u on o.user_id = u.id left join address_book ab on \n" +
            "  o.address_book_id = ab.id left join order_detail od on o.id = od.order_id left join dish d on od.dish_id = d.id left join setmeal s on od.setmeal_id = s.id left join \n" +
            "  setmeal_dish sc on s.id = sc.setmeal_id and od.dish_id = sc.dish_id left join merchant m on o.merchant_id = m.id where u.id = #{id} and  o.status=7 or o.status=8 or o.status=9 order by o.order_time desc; ")
    List<Orders> ShowTuiKuan(@Param("id")int id);
    //查询进行中订单
    @Select(" select o.id as o_id, o.number as o_number , o.refund_reason,o.rejection_reason,o.status , u.name as u_name, u.phone , m.name as m_name, m.contact_phone, o.amount as o_amount , o.order_time , \n" +
            "o.checkout_time , o.pay_method , o.pay_status , o.remark , o.cancel_reason , o.rejection_reason , o.cancel_time , o.estimated_delivery_time , o.delivery_status ,\n" +
            " o.delivery_time , o.pack_amount , o.tableware_number , o.tableware_status ,o.pingjia as ping_jia, od.dish_id , d.name as d_name , d.price as d_price , d.image , od.number , od.amount , \n" +
            " od.dish_flavor , s.id , s.name , s.price , sc.copies, ab.longitude as ulongitude,m.longitude as mlongitude,m.Latitude as mLatitude, ab.latitude as ulatitude, ab.consignee, ab.sex, ab.phone, ab.city_name, ab.district_name, ab.detail, ab.label,\n" +
            "  ab.is_default, m.rejection_reason, m.operational_reason, d.description, d.id as d_id from orders o left join user u on o.user_id = u.id left join address_book ab on \n" +
            "  o.address_book_id = ab.id left join order_detail od on o.id = od.order_id left join dish d on od.dish_id = d.id left join setmeal s on od.setmeal_id = s.id left join \n" +
            "  setmeal_dish sc on s.id = sc.setmeal_id and od.dish_id = sc.dish_id left join merchant m on o.merchant_id = m.id where u.id = #{id} and  o.status=2 or o.status=3 or o.status=4 and pay_status=1 order by o.order_time desc; ")
    List<Orders> ShowProgress(@Param("id") int id);
    //客户取消订单
    @Update("update  orders set status=6 ,cancel_reason=#{cancel_reason} ,cancel_time =now()  where  number=#{id} ")
    Boolean cancelOrder(@Param("cancel_reason") String cancel_reason ,@Param("id") int id);

    @Update("update orders set pay_status = #{pay_status} where id = #{id}")
    Boolean refundOrder(List<Orders> orders);

    @Insert("insert into orders (refund_reason) values(#{refund_reason})")
    Boolean addOrders();

    @Update("update  orders set  status=1 ,dish_quantity=2   where   number=#{outTradeNo}")
    Boolean updState(@Param("outTradeNo") String outTradeNo);

       @Select("select id,number,status,user_id,address_book_id,order_time,checkout_time,pay_method,pay_status,amount,remark,cancel_reason,rejection_reason,cancel_time,estimated_delivery_time,delivery_status,delivery_time,pack_amount,tableware_number,tableware_status from orders where number=#{ordersNumber}")
    Orders getByOrdersNumber(String ordersNumber);
//修改订单 评论
    @Update("update orders set pingjia=1 where id=#{od} ")
    Boolean updatepinglun(@Param("od") String od);

    //根据商家id查询订单
    List<Orders> getOrdersByUserId(int merchant_id);
    //修改状态
    @Update("update orders set `status`=#{status}  \n" +
            "where merchant_id=#{merchant_id}   ")
    int updateStatus(@Param("status") int status,@Param("merchant_id") int merchant_id);
    //查询商家当日订单
    List<Orders> getTodayOrders(@Param("merchant_id") int merchant_id);

//    @Insert("INSERT INTO orders (id,number, status, user_Id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, tableware_Number,merchant_id) " +
//            "VALUES (#{id},#{number}, #{status}, #{user_Id}, #{address_book_id}, #{order_time} ,#{checkout_time}, #{pay_method}, #{pay_status}, #{amount}, #{remark}, #{tableware_number},#{merchant_id})")
    Boolean insertOrdersBy(Orders orders);
       @Update("update  orders set  status=7 , refund_reason=#{refund_reason} where number=#{oid} and pay_status=1  ")
    Boolean Tuikuan(@Param("oid") String oid,@Param("refund_reason") String refund_reason);
    //订单月销售额明细
    List<Orders> selectMerchantMonthSalesDetail(@Param("merchant_id") int merchant_id);
}

